class IPositionChangeListener{
public:
	virtual void PrePositionChange(int oldIndex, int newIndex) = 0;
	virtual void OnPositionChanged(int oldIndex, int newIndex) = 0;
	virtual void PrePositionRemove(int oldIndex) = 0;
	virtual void OnPositionRemoved(int oldIndex) = 0;
};

class IPosition {
public:
	virtual int GetIndex() = 0;
	virtual void SetIndex(int index) = 0;
	virtual bool IsEnable() = 0;
	virtual bool Unique() = 0;
	virtual void Disable() = 0;
};

class Position: public IPosition {
public:
	explicit inline Position(int index = 0) {
		mIndexHolder = long(new int(index));
		mRefHolder = long(new int(1));
	}
	inline Position(const Position& other) {
		mIndexHolder = other.mIndexHolder;
		mRefHolder = other.mRefHolder;
		++GetCountRef();
	}
	inline Position& operator=(const Position& other) {
		Release();
		mIndexHolder = other.mIndexHolder;
		mRefHolder = other.mRefHolder;
		++GetCountRef();
		return *this;
	}
	virtual ~Position() {
		Release();
	}
public:
	virtual inline int GetIndex() {
		return GetIndexRef();
	}
	virtual inline void SetIndex(int index) {
		GetIndexRef() = index;
	}
	virtual inline bool IsEnable() {
		return GetIndex() >= 0;
	}
	virtual inline bool Unique() {
		return GetCountRef() == 1;
	}
	virtual inline void Disable() {
		SetIndex(-1);
	}

	void dump(){
		cout<<"Value: "<<GetIndexRef()<<" \tRef:"<<GetCountRef()<<endl;
	}
private:
	inline int& GetCountRef() {
		return *((int*) mRefHolder);
	}
	inline int& GetIndexRef() {
		return *((int*) mIndexHolder);
	}
	void Release() {
		int& count = GetCountRef();
		--count;
		if (count <= 0) {
			delete &GetIndexRef();
			delete &count;
			mIndexHolder = 0;
			mRefHolder = 0;
		}
	}
private:
	long mIndexHolder;//save the addres of the value(index number), thus, Position copy is enabled.
	long mRefHolder;//save the reference count of the Position
};
